﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "Interview" here, you must also update the reference to "Interview" in App.config.
    public partial class MainService : IInterview
    {
        public DataSet Interview_GetByCandidate(int _CandidateID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_GetByGrade", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CandidateID", _CandidateID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public int Interview_Using(int InterviewID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@InterviewID", InterviewID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Interview_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Interview_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public InterviewInfo Interview_GetInfo(int InterviewID)
        {
            InterviewInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Interview_ID", InterviewID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new InterviewInfo();
                    retVal.ID = Convert.ToInt32(dr["Interview_ID"]);
                    retVal.Person = dr["Interview_Name"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Interview_Insert(InterviewInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Interview_PositionID", eInfo.PositionID);
            dbCmd.Parameters.AddWithValue("@Interview_Person", eInfo.Person);
            dbCmd.Parameters.AddWithValue("@Interview_Approved", eInfo.Approved);
            dbCmd.Parameters.AddWithValue("@Interview_Creteria", eInfo.Creteria);
            dbCmd.Parameters.AddWithValue("@Interview_Mark", eInfo.Mark);
            dbCmd.Parameters.AddWithValue("@Interview_Comments", eInfo.Comments);
            dbCmd.Parameters.AddWithValue("@Interview_CandidateID", eInfo.CandidateID);
            if (eInfo.InterviewDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Interview_Date", eInfo.InterviewDate);
            else
                dbCmd.Parameters.AddWithValue("@Interview_Date", DBNull.Value);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Interview_Update(InterviewInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Interview_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Interview_PositionID", eInfo.PositionID);
            dbCmd.Parameters.AddWithValue("@Interview_Person", eInfo.Person);
            dbCmd.Parameters.AddWithValue("@Interview_Approved", eInfo.Approved);
            dbCmd.Parameters.AddWithValue("@Interview_Creteria", eInfo.Creteria);
            dbCmd.Parameters.AddWithValue("@Interview_Mark", eInfo.Mark);
            dbCmd.Parameters.AddWithValue("@Interview_Comments", eInfo.Comments);
            dbCmd.Parameters.AddWithValue("@Interview_CandidateID", eInfo.CandidateID);
            if (eInfo.InterviewDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Interview_Date", eInfo.InterviewDate);
            else
                dbCmd.Parameters.AddWithValue("@Interview_Date", DBNull.Value);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Interview_Delete(int InterviewID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Interview_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Interview_ID", InterviewID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
